import axios from "axios";
// 引入 store
// import store from "@/store";
// import { token } from '@/store/login.js'
import { Message } from "@arco-design/web-vue";
const request = axios.create({
  baseURL: "http://localhost:3001",
  timeout: 1000 * 10,
});

// 请求拦截
request.interceptors.request.use(
  (config) => {
    // 获取 token
    // 在组件中使用 this.$store === store
    // const token = token
    // if (token) {
    //   // token 存在，就设置给headers
    //   config.headers.token = token;
    // }

    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截
request.interceptors.response.use(
  (response) => {
    const { data } = response;
    if (data.code === "200") {
      return data;
    } else {
      Message.error(data.Message || "网络异常，请稍后重试！");
      throw new Error(data.Message || "网络异常，请稍后重试！");
    }
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 将 request 导出
export default request;
